// pages/order-info/order-info.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    condition: null,
    totalPrice: null,
    alltotalPrice: null,
    hiddenmodalput: true,
    hidden: true,
    hiddenpp: false,
    hiddenmm: true,
  },
  chat() {
    wx.navigateTo({
      url: '/pages/customer/customer',
    })
  },
  info(e) {
    var id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '/pages/order-tracking/order-tracking?id=' + id + '+',
    })
  },
  iCommend(e) {
    // console.log('commend', e.detail.value)
    this.setData({
      commend: e.detail.value
    })
  },
  cancelMCommend: function (e) {
    this.setData({
      hidden: true,
    })
  },
  confirmMCommend: function (e) {
    var commend = this.data.commend
    var userid = this.data.arraa[0].id
    var goodsid = this.data.arrsearch[0].goodsid
    var userimg = this.data.arraa[0].img
    var petname = this.data.arraa[0].petname

    var time = util.formatTime(new Date());
    console.log(time)
    var that = this
    wx.request({
      url: 'http://localhost/shopping/commend_add.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        commend: commend,
        goodsid: goodsid,
        userid: userid,
        userimg: userimg,
        petname: petname,
        time: time,
      },
      success: function (res) {
        // console.log(res.data)
        wx.showToast({
          title: '发表评论成功',
          icon: 'success',
          duration: 2000
        })
        that.setData({})
      },
      fail: function (res) {
        console.log("fail");
      }
    })

    this.setData({
      hidden: true,
    })
  },

  radiochange(e) {
    console.log('usercouponid', e.detail.value)
    this.setData({
      usercouponid: e.detail.value
    })
  },

  cancelM: function (e) {
    this.setData({
      hiddenmodalput: true,
      hidden: true,
    })
  },
  confirmM: function (e) {
    var that = this
    wx.request({
      url: 'http://localhost/shopping/user_coupon_search.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: that.data.usercouponid
      },
      success: function (res) {
        console.log(res.data)
        var coupontype = res.data.usercouponid[0].coupontype
        var priceone = res.data.usercouponid[0].priceone
        var pricetwo = res.data.usercouponid[0].pricetwo

        var startdate = res.data.usercouponid[0].startdate
        var enddate = res.data.usercouponid[0].enddate
        var startdatetime = Date.parse(startdate) / 1000
        var enddatetime = Date.parse(enddate) / 1000
        var time = util.formatTime(new Date());
        var nowtime = Date.parse(time) / 1000
        console.log(startdatetime, nowtime, enddatetime)
        console.log(that.data.type, coupontype)
        console.log(that.data.alltotalPrice, priceone)
        if (that.data.type == coupontype && Number(that.data.alltotalPrice) >= Number(priceone)) {
          console.log('满足使用要求')
          if (nowtime >= startdatetime && nowtime <= enddatetime) {
            console.log('时间满足使用要求')
            that.setData({
              alltotalPrice: (that.data.alltotalPrice - pricetwo).toFixed(2)
            })
          }
        } else {
          console.log('不满足使用要求')
          wx.showModal({
            title: '',
            content: '不满足使用要求',
          })
        }
        that.setData({})
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    that.setData({
      hiddenmodalput: true
    })
  },


  coupon(e) {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })

  },
  condition(e) {
    let name = getApp().globalData.usersname
    var condition = e.currentTarget.dataset.condition

    var id = e.currentTarget.dataset.id
    var that = this;
    console.log(condition)
    if (condition == '支付') {
      var time = util.formatTime(new Date());

      var money = that.data.arraa[0].money
      console.log(money, that.data.totalPrice, that.data.alltotalPrice)
      if (money < that.data.alltotalPrice) {
        console.log('你的余额不足，无法付款')
      } else {
        console.log('余额充足')
        wx.showModal({
          title: '',
          content: '是否进行付款',
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后
              console.log('确认付款')
              wx.request({
                url: 'http://localhost/shopping/users_money_pay.php',
                header: {
                  'Content-Type': 'application/json'
                },
                data: {
                  username: name,
                  alltotalPrice: that.data.alltotalPrice,
                  money: money,
                },
                success: function (res) {
                  console.log(res.data)
                  wx.request({
                    url: 'http://localhost/shopping/order_pay_edit.php',
                    header: {
                      'Content-Type': 'application/json'
                    },
                    data: {
                      startdate: time,
                      id: id,
                      alltotalPrice: that.data.alltotalPrice,
                    },
                    success: function (res) {
                      console.log(res.data)
                      wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 2000
                      })

                    },
                    fail: function (res) {
                      console.log("fail");
                    }
                  })

                },
                fail: function (res) {
                  console.log("fail");
                }
              })
            } else { //这里是点击了取消以后
              console.log('取消')
            }
          }
        })

      }


    } else if (condition == '分享') {
      wx.showModal({
        title: '',
        content: '是否分享给朋友',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('分享')
            wx.showToast({
              title: '分享成功',
              icon: 'success',
              duration: 2000
            })
          } else { //这里是点击了取消以后
            console.log('取消')
          }
        }
      })
    } else if (condition == '催发货') {
      wx.showModal({
        title: '',
        content: '是否向商家发送发货请求',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('请求')
            wx.showToast({
              title: '请求成功',
              icon: 'success',
              duration: 2000
            })
          } else { //这里是点击了取消以后
            console.log('取消')
          }
        }
      })
    } else if (condition == '收货') {
      wx.showModal({
        title: '',
        content: '是否确认收货',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('确认')
            wx.request({
              url: 'http://localhost/shopping/order_receiving_edit.php',
              header: {
                'Content-Type': 'application/json'
              },
              data: {
                id: id
              },
              success: function (res) {
                // console.log(res.data)
                wx.showToast({
                  title: '成功收货',
                  icon: 'success',
                  duration: 2000
                })
              },
              fail: function (res) {
                console.log("fail");
              }
            })
          } else { //这里是点击了取消以后
            console.log('取消')
          }
        }
      })
    } else if (condition == '评价') {
      that.setData({
        hidden: !that.data.hidden
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    // console.log(e.ordernumber)
    var id = e.id
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/order_info.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: id
      },
      success: function (res) {
        if (res.data.orderssearch[0].condition == '待付款') {
          that.data.condition = '支付'
          that.setData({
            hiddenmm: !that.data.hiddenmm,
          })
        } else if (res.data.orderssearch[0].condition == '待分享') {
          that.data.condition = '分享'
        } else if (res.data.orderssearch[0].condition == '待发货') {
          that.data.condition = '催发货'
        } else if (res.data.orderssearch[0].condition == '待收货') {
          that.data.condition = '收货'
        } else if (res.data.orderssearch[0].condition == '待评价') {
          that.data.condition = '评价'
        }
        if (res.data.orderssearch[0].tracking == null) {
          that.setData({
            hiddenpp: !that.data.hiddenpp,
          })
        }
        that.setData({
          arrsearch: res.data.orderssearch,
          type: res.data.orderssearch[0].type,
          totalPrice: Number(res.data.orderssearch[0].goodsprice * res.data.orderssearch[0].goodsnumber),
          condition: that.data.condition,
          alltotalPrice: Number(res.data.orderssearch[0].goodsprice * res.data.orderssearch[0].goodsnumber),
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    let name = getApp().globalData.usersname
    wx.request({
      url: 'http://localhost/shopping/users.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          arraa: res.data.users,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/user_coupon.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        usersname: name
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrusercoupon: res.data.usercoupon,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    this.setData({
      totalPrice: totalPrice,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})